package edu.gatech.cs2340.group7.wheresmystuffapp.views;

//import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.IReportItemView;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.ReportDonationItemPresenter;

public class ReportDonationItemView extends IReportItemView implements OnClickListener,
		OnCheckedChangeListener {
	private ReportDonationItemPresenter myPresenter;
	private String PACKAGE_NAME;
	//private final static int cameraData = 0; //To Do
	private EditText etItemName;
	private EditText etItemDescription;
	private EditText etDateDonated;
	private Button bCancel;
	private Button bAddItem;
	private Button bTakePic;
	private Button bCurrentLocation;
	private Button bSelectLocation;
	private TextView tvItemDonationBanner;
	private byte[] pic;
	//private Intent cameraIntent; //To Do
	private RadioGroup rgCategory;
	private static final int SELECT_PICTURE = 1;
	private String selectedImagePath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_report_donation_item);

		initialize();
		PACKAGE_NAME = getApplication().getPackageName();
		myPresenter = new ReportDonationItemPresenter(this);
		category = "empty";
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_report_donation_item, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Method called when the user pushes a button on the Report donation Item
	 * Activity
	 * 
	 * @param view
	 *            The view that was clicked
	 */
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.b_upload_donation_pic:
			addPic();
			// Allows the user to take a picture
			break;
		case R.id.b_add_donation_item:
			addItem();
			break;
		case R.id.b_cancel_from_donation:
			Intent intent = new Intent(this, UserMainMenuView.class);
			intent.setPackage(PACKAGE_NAME);
			startActivity(intent);
			// Takes user back to Main Menu
			break;
		case R.id.b_donate_item_location_current:
			location = myPresenter.getCurrentLocation();
			break;
		case R.id.b_donate_item_location_select:
			String name = etItemName.getText().toString();
			String description = etItemDescription.getText().toString();
			String activity = "Donate";
			myPresenter.selectLocation(name, description, category, activity);
			break;
		}

	}

	/**
	 * Method called to allow the user to take a picture Follow NewBoston video
	 * for implementation
	 */
	private void addPic() {

		// cameraIntent = new
		// Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		// startActivityForResult(cameraIntent, cameraData);

		Intent cameraIntent = new Intent();
		cameraIntent.setType("image/*");
		cameraIntent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(
				Intent.createChooser(cameraIntent, "Select Picture"),
				SELECT_PICTURE);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_PICTURE) {
				Uri selectedImageUri = data.getData();
				selectedImagePath = getPath(selectedImageUri);
				System.out.println("Image Path : " + selectedImagePath);

			}
		}
	}

	@SuppressWarnings("deprecation")
	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	/**
	 * 
	 * @return  -1 if the item was not added
	 */
	public long addItem() {
		long out = -1;
		String itemName = etItemName.getText().toString();
		String description = etItemDescription.getText().toString();
		String dateDonated = etDateDonated.getText().toString();
		
		out = myPresenter.addItem(itemName, description, category, dateDonated,
				location, pic);
		return out;
	}

	public String getPACKAGE_NAME() {
		return PACKAGE_NAME;
	}

	private void initialize() {
		etItemName = (EditText) findViewById(R.id.et_item_donation_name);
		etItemName.setText(name);

		etItemDescription = (EditText) findViewById(R.id.et_item_donation_description);
		etItemDescription.setText(description);
		
		etDateDonated = (EditText) findViewById(R.id.et_date_donated);
		bCurrentLocation = (Button) findViewById(R.id.b_donate_item_location_current);
		bSelectLocation = (Button) findViewById(R.id.b_donate_item_location_select);
		bCancel = (Button) findViewById(R.id.b_cancel_from_donation);
		bAddItem = (Button) findViewById(R.id.b_add_donation_item);
		bTakePic = (Button) findViewById(R.id.b_upload_donation_pic);
		tvItemDonationBanner = (TextView) findViewById(R.id.tv_report_donation_banner);
		rgCategory = (RadioGroup) findViewById(R.id.radioItemTypeDonation);

		rgCategory.setOnCheckedChangeListener(this);
		bCancel.setOnClickListener(this);
		bAddItem.setOnClickListener(this);
		bTakePic.setOnClickListener(this);
		bCurrentLocation.setOnClickListener(this);
		bSelectLocation.setOnClickListener(this);
	}

	public void setAddItemResult(String result) {
		tvItemDonationBanner.setText(result);
		tvItemDonationBanner.setTextColor(Color.GREEN);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.radioCategoryKeepsakes:
			category = "Keepsakes";
			break;
		case R.id.radioCategoryHeirlooms:
			category = "Heirlooms";
			break;
		case R.id.radioCategoryMisc:
			category = "Misc";
			break;
		}

	}
}
